# -*- coding: utf-8 -*-
# @Time     : 2021/12/4 21:51
# @Author   : wljess
# @File     : 自定义序列类.py
# @Describe : 
# @Software : PyCharm


class Group:

    def __init__(self, group_name, company_name, staffs):
        print("__init__")
        self.group_name = group_name
        self.company_name = company_name
        self.staffs = staffs

    def __getitem__(self, item):
        print("__getitem__")
        cls = type(self)
        if isinstance(item, slice):
            return cls(self.group_name, self.company_name, self.staffs[item])
        elif isinstance(item, int):
            return cls(self.group_name, self.company_name, [self.staffs[item]])
        else:
            raise KeyError("请传入切片或者索引类型数据...")

    def __setitem__(self, key, value):
        print("__setitem__")

        if isinstance(key, int) and key < len(self.staffs):
            self.staffs[key] = value
        # try:
        #     if isinstance(key, int) and key < len(self.staffs):
        #         self.staffs[key] = value
        # except KeyError as error:
        #     print(error)
        # except IndexError as indexError:
        #     print(indexError)
        # except TypeError as error:
        #     print(error)

    def __delitem__(self, key):
        print("__delitem__")
        del self.staffs[key]

    def __len__(self):
        print("__len__")
        return len(self.staffs)

    def __reversed__(self):
        print("__reversed__")
        return self.staffs.reverse()

    def __iter__(self):
        print("__iter__")
        return iter(self.staffs)

    def __contains__(self, item):
        print("__contains__")
        if item in self.staffs:
            return True
        else:
            return False


if __name__ == '__main__':
    staffs = ["bobby1", "imooc", "bobby2", "bobby3"]
    group = Group("imooc", "user", staffs)
    group[0] = "abc"
    group[100] = "efg"
    group["name"] = "李白"
    print("---------------------", len(group))
    print("group[0]--------------->", group[0])  # __getitem__ 中的item 为  int  0
    print("group[:2]------------>", group[:2])  # __getitem__ 中的item 为  slice 类型  slice(None, 2, None)
    # print("group['a']------------>", group['a'])
    reversed(group)
    for user in group:
        print(user)
